package stickyhand;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.filter.ElementFilter;
import org.jdom.DataConversionException;


public class NZBFile {
	private String poster;
	private String date;
	private String name;
	private LinkedList<NZBSegment> segList;
	private LinkedList<NZBGroup> groupList;
	
	public NZBFile(Element e) {
		List attributeList = e.getAttributes();
		Iterator attributeIterator = attributeList.iterator();
		while (attributeIterator.hasNext()) {
			Attribute attribute = (Attribute) attributeIterator.next();
			if (attribute.getName().equals("subject")) {
				name = attribute.getValue();
			} else if (attribute.getName().equals("date")) {
				date = attribute.getValue();
			} else if (attribute.getName().equals("poster")) {
				poster = attribute.getValue();
			}
		}
		
		segList = new LinkedList<NZBSegment>();
		Iterator elementList = e.getDescendants(new ElementFilter("segments"));
		while (elementList.hasNext()) {
			Element nextElement = (Element)elementList.next();
			Iterator subElementList = nextElement.getDescendants(new ElementFilter("segment"));
			while (subElementList.hasNext()) {
				try {
					NZBSegment seg = new NZBSegment((Element)subElementList.next());
					segList.add(seg);	// TODO: assumes added in same order as segement:number attribute value
				} catch (DataConversionException dce) {
					// TODO: add a warning or keep track of a statistic about this.
				}
			}
		}
		
		groupList = new LinkedList<NZBGroup>();
		elementList = e.getDescendants(new ElementFilter("groups"));
		while (elementList.hasNext()) {
			Element nextElement = (Element)elementList.next();
			Iterator subElementList = nextElement.getDescendants(new ElementFilter("group"));
			while (subElementList.hasNext()) {
				NZBGroup group = new NZBGroup((Element)subElementList.next());
				groupList.add(group);
			}
		}
	}
	
	/**
	 * @return the poster
	 */
	public String getPoster() {
		return poster;
	}

	/**
	 * @return the date
	 */
	public String getDate() {
		return date;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	
	public int getBytes() {
		int totalBytes = 0;
		
		Iterator<NZBSegment> segments = segList.iterator();
		while (segments.hasNext()) {
			totalBytes += segments.next().getBytes();
		}
		return totalBytes;
	}
	
	public int getNumSegments() {
		return segList.size();
	}
	
	public LinkedList<NZBGroup> getGroupList() {
		return (LinkedList<NZBGroup>)groupList.clone();
	}
	
	public String toString() {
		StringBuilder result = new StringBuilder();
		result.append("name: " + name + "\n");
		result.append("  poster: " + poster +  "\n");
		result.append("  date:   " + date + "\n");
		
		result.append("  groups: \n");
		Iterator groups = groupList.iterator();
		while (groups.hasNext()) {
			result.append("    " + (NZBGroup)groups.next() + "\n");
		}
		
		result.append("  segments: \n");
		Iterator segments = segList.iterator();
		while (segments.hasNext()) {
			result.append("    " + (NZBSegment)segments.next() + "\n");
		}
		
		return result.toString();
	}
}
